package com.filebackup.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.filebackup.dao.CloudMappingDao;
import com.filebackup.vo.Vo;
import com.filebackup.vo.impl.CloudMappingVo;
import com.filebackup.vo.impl.CloudVo;
import com.filebackup.vo.impl.LocalFileVo;

public class CloudMappingDaoImpl implements CloudMappingDao {

	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public boolean addVo(Vo vo) {
		
		this.getSessionFactory().getCurrentSession().save(vo);
		return false;
	}

	public boolean deleteVo(Vo vo) {
		this.getSessionFactory().getCurrentSession().delete(vo);
		return false;
	}

	public List<Vo> findAll() {
		Query query = this.getSessionFactory().getCurrentSession().createQuery("from CloudMappingVo");
		List<Vo> voList = query.list();
		return voList;
	}

	public Vo findByPK(Object pk) {
		Vo vo = (Vo) this.getSessionFactory().getCurrentSession().get(CloudMappingVo.class, (Integer)pk);
		return vo;
	}

	public boolean updateVo(Vo vo) {
		this.getSessionFactory().getCurrentSession().update(vo);
		return false;
	}

	public Vo findByCloudId(int cloudId) {
		// TODO Auto-generated method stub
		CloudMappingVo lvo=(CloudMappingVo)this.getSessionFactory().getCurrentSession().createQuery("from CloudMappingVo where cloudId=:cloudId").setInteger("cloudId", cloudId).uniqueResult();
		return lvo;
	}

	public Vo findByLocalId(int type,int localId) {
		// TODO Auto-generated method stub
		Query query=this.getSessionFactory().getCurrentSession().createQuery("from CloudMappingVo where localId=:localId").setInteger("localId", localId);
		Vo vo=null;
		List<Vo> voList=query.list();
		for(int i=0;i<voList.size();i++){
			if(((CloudMappingVo)voList.get(i)).getCloudVo().getCloudType().intValue()==type){
				vo=voList.get(i);
			}
		}
		return vo;
	}

}
